package com.bw.module_service.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.lib_common.arouter.Router
import com.bw.module_service.R
import com.bw.module_service.model.AnnouncementModelItem


class AnnouncementAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    private val list = mutableListOf<AnnouncementModelItem>()
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val itemView = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_announcement,parent,false)
        return ViewHolder(itemView)
    }


    interface OnItemClickListener {
        fun onItemClick(position: Int, item: AnnouncementModelItem)
    }

    private var listener: OnItemClickListener? = null


    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.listener = listener
    }



    fun setData(list: List<AnnouncementModelItem>){
        this.list.clear()
        this.list.addAll(list)
        notifyDataSetChanged()
    }


    override fun getItemCount(): Int {
        return list.size
    }


    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val customHolder = holder as AnnouncementAdapter.ViewHolder
        customHolder.title.text = list[position].title
        customHolder.time.text = list[position].publishTime
        customHolder.content.text = list[position].content

        if(customHolder.status.text == null){
            customHolder.status.text = "敬请期待"
        }else{
            customHolder.status.text = list[position].status
        }


    }


    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val title: TextView = itemView.findViewById(R.id.announcement_title)
        val time: TextView = itemView.findViewById(R.id.announcement_time)
        val content: TextView = itemView.findViewById(R.id.announcement_connect) // 补充 content
        val status:TextView = itemView.findViewById(R.id.announcement_status)

        init {
            itemView.setOnClickListener {
                val position = adapterPosition
                if (position != RecyclerView.NO_POSITION) {
                    listener?.onItemClick(position, list[position])
                }
            }
        }
    }
}